<?php
namespace App\Query;

use App\Model\MallGoods;

class MallGoodsQuery extends \HyperfStore\Query
{
    // 构建模型
    protected static function build()
    {
        return MallGoods::orderBy('sort', 'desc')->orderBy('updated_at', 'desc');
    }
    
    // 渴望加载
    protected static function with()
    {
        return ['mall', 'shop'];
    }
    
    // 有货状态并且上架
    public static function status($model = null)
    {
        return self::model($model)->where('status', MallGoods::STATUS_OK)->where('count', '>', 0);
    }
    
    // 所属商家
    public static function shop($shop)
    {
        return static::model()->where('shop_id', $shop->id);
    }
    
    // 查询需要现金支付的
    public static function queryMoneyPay($model, $where)
    {
        return $where ? $model->where('money_pay', '>', 0) : $model;
    }
    
    // 查询需要购物积分的
    public static function queryMoneyShop($model, $where)
    {
        return $where ? $model->where('money_shop', '>', 0) : $model;
    }
    
    // 查询需要兑换积分的
    public static function queryMoneyPoint($model, $where)
    {
        return $where ? $model->where('money_point', '>', 0) : $model;
    }
}